package com.dearbinge.opentapi.content.uweShare;

import com.dearbinge.data.api.uweShare.UweShareService;
import com.dearbinge.data.pojo.uweShare.UweShare;
import com.dearbinge.utils.ResultMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.List;
import java.util.Map;

@Api(description = "分享表")
@RestController
@RequestMapping("/uweShare")
@EnableAutoConfiguration
public class UweShareController extends WebMvcConfigurerAdapter {

    @Autowired
    private UweShareService uweShareService;

    /**
     * 新增/修改
     */
    @ApiOperation(value = "新增/修改",notes = "新增/修改")
    @RequestMapping(value = "addOrUpdate",method = RequestMethod.POST)
    public ResultMsg<String> addOrUpdate(@RequestBody UweShare uweShare){
        try {
            if (uweShare.getId() != null && uweShare.getId() != 0){
                Integer dataCode = uweShareService.update(uweShare);
                if (dataCode > 0){
                    return ResultMsg.ok("修改成功");
                }
            }else{
                Integer dataCode = uweShareService.insert(uweShare);
                if (dataCode > 0){
                    return ResultMsg.ok("新增成功");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 根据ID删除
     */
    @ApiOperation(value = "根据ID删除",notes = "根据ID删除")
    @ApiImplicitParam(name = "id",value = "id",paramType = "query")
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public ResultMsg<String> delete(Integer id){
        try {
            Integer dataCode = uweShareService.delete(id);
            if (dataCode > 0){
                return ResultMsg.ok("删除成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 根据ID查询
     */
    @ApiOperation(value = "根据ID查询全部",notes = "根据ID查询")
    @ApiImplicitParam(name = "id",value = "id",paramType = "query")
    @RequestMapping(value = "/getById",method = RequestMethod.GET)
    public ResultMsg<Map<String,Object>> getById(Integer id){
        try {
            Map<String,Object> uweShare = uweShareService.getById(id);
            return ResultMsg.ok(uweShare);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 根据id与类型查询全部的分享
     */
    @ApiOperation(value = "根据ID与类型查询全部的分享",notes = "根据ID与类型查询全部")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "id",paramType = "query"),
            @ApiImplicitParam(name = "fromType",value = "来源（1：秀场  2：生活  3：商品）",paramType = "query")})
    @RequestMapping(value = "/getByIdAndFromType",method = RequestMethod.GET)
    public ResultMsg<List<Map<String,Object>>> getByIdAndFromType(Integer id, Integer fromType){
        try {
            List<Map<String,Object>> list = uweShareService.getByIdAndFromType(id,fromType);
            return ResultMsg.ok(list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

}
